package com.milks.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;




import com.milks.dao.IUserDao;
import com.milks.entity.Users;
import com.milks.service.IUserService;


@Service("userService")
public class UserServiceImpl implements IUserService {
	private IUserDao userHibernateDao;
	
	public IUserDao getUserHibernateDao() {
		return userHibernateDao;
	}

	@Resource
	public void setUserHibernateDao(IUserDao userHibernateDao) {
		this.userHibernateDao = userHibernateDao;
	}


	@Override
	public void add(Users u) {
		
		userHibernateDao.add(u);
	}

	@Override
	public void delete(int id) {
		userHibernateDao.delete(id);
	}

	@Override
	public void update(Users u) {
		userHibernateDao.update(u);
	}

	@Override
	public Users load(int id) {
		return userHibernateDao.load(id);
	}

	@Override
	public List<Users> listAllUser() {
		String hql = "from User";
		return userHibernateDao.list(hql);
	}

	@Override
	public List<Users> listByGroup(int gid) {
		return userHibernateDao.list("from Users where group.id=?", gid);
	}
	
	
	public Users login(String username, String password) {
		Users u = userHibernateDao.login(username,password);
		//if(u==null) throw new UserException("登录用户不存在");
		//if(!u.getPassword().equals(password)) throw new UserException("用户密码不正确");
		return u;
	}

	@Override
	public Users loadByUsername(String username) {
		Users u = userHibernateDao.loadByUsername(username);
		return u;
	}
	/**
	 * 通过邮箱获取用户对象
	 * @param email
	 * @return Users
	 */
	@Override
	public Users loadByEmail(String email) {
		Users u = userHibernateDao.loadByEmail(email);
		return u;
	}

}
